package Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import DAO.BoxDao;
import POJO.Box;
import POJO.Terminal;

@Controller
@RequestMapping(value="heartBeat")
public class HeartBeatController {
//	static Logger logger = Logger.getLogger(HeartBeatController.class);
	@Autowired
	BoxDao boxDao;
	
	@SuppressWarnings("unchecked")
	@RequestMapping
	@ResponseBody
	public Map<String,Object> heartBeat(HttpSession httpSession,
										HttpServletRequest httpRequest){
//		logger.info("heartbeat");
		HashMap<String,Object> response=new HashMap<String,Object>();
		Terminal terminal =(Terminal)httpSession.getAttribute("terminal");
		ArrayList<Box> boxes=boxDao.returnForceOpenBoxes(terminal);
		if(boxes!=null){
			for(Box b:boxes){
				boxDao.updateForceOpenBoxStatus(terminal, b.getBoxSeq());
			}
			response.put("result", "0");
			response.put("forceOpen", boxes);
		}else{
			 response=(HashMap<String, Object>) httpRequest.getServletContext().getAttribute("terminal"+terminal.getTerminalID());
			if(response==null){
				//无任何消息
				response=new HashMap<String,Object>();
				response.put("result", "1");
			}else{
				if(response.containsKey("result")){
					if(!response.get("result").equals("5")){
						//系统维护需要保持
						httpRequest.getServletContext().removeAttribute("terminal"+terminal.getTerminalID());
					}
				}
			}
		}
		return response;

	}
}
